﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using HM.UI.HMService;
using GalaSoft.MvvmLight.Command;

namespace HM.UI.ViewModels.Management
{
    class ManagePaymentsViewModel : INotifyPropertyChanged
    {
        private HMServiceClient proxy = new HMServiceClient();

        public ManagePaymentsViewModel()
        {
            this.RefreshPayments();
            SaveChanges = new RelayCommand(SavePaymentsExecute);
            RemoveSelected = new RelayCommand(RemoveSelectedExecute, CanExecuteRemoveSelected);
        }

        #region Refresh

        private void RefreshPayments()
        {
            this.proxy.GetPaymentsCompleted += (s, e) =>
            {
                this.Payments = e.Result;
            };
            this.proxy.GetPaymentsAsync();
        }

        #endregion

        #region Execute

        public RelayCommand SaveChanges { get; private set; }

        private void SavePaymentsExecute()
        {
            if (this.Payments != null)
            {
                this.proxy.SavePaymentsCompleted += (s, e) =>
                {
                    this.RefreshPayments();
                };
                this.proxy.SavePaymentsAsync(this.Payments.ToArray());
            }
        }

        public RelayCommand RemoveSelected { get; private set; }

        private void RemoveSelectedExecute()
        {
            this.proxy.RemovePaymentCompleted += (s, e) =>
            {
                this.RefreshPayments();
            };
            this.proxy.RemovePaymentAsync(this.SelectedPayment);
        }

        #endregion

        #region CanExecute

        private bool CanExecuteRemoveSelected()
        {
            return this.SelectedPayment != null;
        }

        #endregion

        #region Properties

        private IEnumerable<Payment> payments;

        public IEnumerable<Payment> Payments
        {
            get
            {
                return this.payments;
            }

            set
            {
                if (payments != value)
                {
                    this.payments = value;
                    this.OnPropertyChanged("Payments");
                }
            }
        }

        private Payment selectedPayment;

        public Payment SelectedPayment
        {
            get
            {
                return this.selectedPayment;
            }

            set
            {
                if (selectedPayment != value)
                {
                    this.selectedPayment = value;
                    this.OnPropertyChanged("selectedPayment");
                }
            }
        }

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
